home *** CD-ROM | disk | FTP | other *** search
Java Source | 2003-07-02 | 6.5 KB | 196 lines |
- import java.io.*;
- import java.sql.*;
- import java.util.*;
-
- import javax.xml.parsers.*;
-
- import org.w3c.dom.*;
- import org.xml.sax.*;
-
- public class Prodotti extends TabellaDB {
-
- public Prodotti( Connection conn ) {
- super(conn);
- }
-
- void drop() throws SQLException {
- String sql = "DROP TABLE PRODOTTI";
- execute( sql );
- }
-
- void create() throws SQLException {
- String sql = "CREATE TABLE PRODOTTI ("+
- "ID int(11) NOT NULL auto_increment,"+
- "NOME varchar(100) default '',"+
- "DESCRIZIONE text,"+
- "PREZZO float NOT NULL default '0',"+
- "SCONTO float NOT NULL default '0',"+
- "CODICE_IVA int(11) NOT NULL default '0',"+
- "CATEGORIA int(11) NOT NULL default '0',"+
- "IMMAGINE blob,"+
- "LINK varchar(255),"+
- "PRIMARY KEY (id),"+
- "KEY prodotti_idx1 (CATEGORIA,ID),"+
- "KEY prodotti_idx2 (CATEGORIA)"+
- ")";
- execute( sql );
- }
-
- void fill() throws SQLException, Exception {
- String sql = "INSERT INTO PRODOTTI (NOME, DESCRIZIONE, PREZZO, SCONTO, CODICE_IVA,"+
- "CATEGORIA, IMMAGINE, LINK) VALUES (?,?,?,?,?,?,?,?)";
-
- PreparedStatement pstmt = conn.prepareStatement( sql );
-
- DatiProdotto[] dp = loadDatiProdotti();
-
- for( int i=0; i<dp.length; i++ ) {
- int pos = 1;
-
- pstmt.setString( pos++, dp[i].nome );
- pstmt.setString( pos++, dp[i].descrizione );
- pstmt.setFloat( pos++, dp[i].prezzo );
- pstmt.setFloat( pos++, dp[i].sconto );
- pstmt.setInt( pos++, dp[i].codiceIva );
- pstmt.setInt( pos++, dp[i].categoria );
- pstmt.setBytes( pos++, dp[i].immagine );
- pstmt.setString( pos++, dp[i].link );
-
- log( "Prodotti: "+dp[i].nome+", "+dp[i].prezzo );
-
- pstmt.executeUpdate();
- }
- pstmt.close();
-
- }
-
- DatiProdotto[] loadDatiProdotti() throws ParserConfigurationException, SAXException, IOException {
- List elenco = new LinkedList();
- String sourceFile = "prodotti.xml";
-
- DocumentBuilder db = DocumentBuilderFactory.newInstance().newDocumentBuilder();
-
- log("Prodotti: utilizzo il file "+ sourceFile );
-
- File inputFile = new File( sourceFile );
- Document doc = db.parse( inputFile );
- NodeList nl = doc.getElementsByTagName( "prodotto" );
-
- for( int i=0; i<nl.getLength(); i++ ) {
- Node node = nl.item(i);
- DatiProdotto dp = new DatiProdotto();
-
- NamedNodeMap attributi = node.getAttributes();
- dp.nome = attributi.getNamedItem( "nome" ).getNodeValue();
-
- caricaDatiProdotto( node.getChildNodes(), dp );
- System.out.println( dp );
-
- elenco.add( dp );
- }
-
- return (DatiProdotto [])elenco.toArray( new DatiProdotto[ elenco.size() ] );
- }
-
- void caricaDatiProdotto( NodeList lista, DatiProdotto dp ) throws IOException {
- for( int i=0; i<lista.getLength(); i++ ) {
- Node node = lista.item(i);
- String name = node.getNodeName();
-
- if( name.equals("descrizione") ) {
- dp.descrizione = getTextChild( node );
- }
- if( name.equals("prezzo") ) {
- dp.prezzo = parseFloat( getTextChild( node ) );
- }
- if( name.equals("sconto") ) {
- dp.sconto = parseFloat( getTextChild( node ) );
- }
- if( name.equals("codiceIva") ) {
- dp.codiceIva = parseInt( getTextChild( node ) );
- }
- if( name.equals("categoria") ) {
- dp.categoria = parseInt( getTextChild( node ) );
- }
- if( name.equals("link") ) {
- dp.link = getTextChild( node );
- }
- if( name.equals("immagine") ) {
- dp.immagine = loadImage( getTextChild( node ) );
- }
- }
- }
-
- String getTextChild( Node node ) {
- NodeList list = node.getChildNodes();
- for( int i=0; i<list.getLength(); i++ ) {
- Node item = list.item(i);
-
- if( item.getNodeType() == Node.TEXT_NODE ) {
- return item.getNodeValue();
- }
- }
-
- return null;
- }
-
- float parseFloat( String value ) {
- float result = 0;
- try {
- result = Float.parseFloat( value );
- } catch( NumberFormatException ex ) {
- }
- return result;
- }
-
- int parseInt( String value ) {
- int result = 0;
- try {
- result = Integer.parseInt( value );
- } catch( NumberFormatException ex ) {
- }
- return result;
- }
-
- static final int BUF_SIZE = 4096;
-
- byte[] loadImage( String name ) throws IOException {
- String filename = "immagini"+File.separator+name;
- System.out.println( filename );
-
- FileInputStream fis = new FileInputStream(filename);
- ByteArrayOutputStream ba = new ByteArrayOutputStream();
- byte[] buffer = new byte[BUF_SIZE];
- int len;
-
- while( true ) {
- len = fis.read( buffer );
- if( len > 0 ) {
- ba.write( buffer, 0, len );
- } else {
- break;
- }
- }
-
- return ba.toByteArray();
- }
-
- class DatiProdotto {
- public String nome;
- public String descrizione;
- public float prezzo;
- public float sconto;
- public int codiceIva;
- public int categoria;
- byte[] immagine;
- public String link;
-
- public String toString() {
- String result = "DatiProdotto=[nome="+nome+",\ndescrizione="+descrizione+
- ",\nprezzo="+prezzo+",\n sconto="+sconto+",\n codiceIva="+codiceIva+
- ",\ncategoria="+categoria+",\nimmagine="+immagine+",\n link="+link;
- return result;
- }
- }
- }
-